package com.sali.design_pattern.c_factory_method_pattern.lesson_03.service;

import com.sali.design_pattern.c_factory_method_pattern.lesson_03.pojo.User;
import com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.vip.IUserVipService;
import com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.vip.NormalUserService;
import com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.vip.SVIPUserService;
import com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.vip.VIPUserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;

/**
 * @description: （大量if else出现）用接口抽象出逻辑
 * @author: zhang hang
 * @date: 2022/11/5 09:39.
 * @version: 1.0
 */
@Service
@Slf4j
public class UserServiceImpl2 implements com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.IUserService {

    @Override
    public User getUser(Long id) {
        User user = User.builder().id(1L).vip(3).username("张萨丽").password("123456").build();
        return user;
    }

    @Override
    public String buyCourse(Long userId, Long courseId) {
        User user = this.getUser(userId);
        if (user != null) {
            Integer vip = user.getVip();

            IUserVipService userVipService = null;

            // 此处破坏了开闭原则
            if (vip.equals(3L)) {
                userVipService = new SVIPUserService();
            } else if (vip.equals(2L)) {
                userVipService = new VIPUserService();
            } else {
                userVipService = new NormalUserService();
            }

            Double money = userVipService.getMoney(100D);
            Integer corn = userVipService.getCorn();
            // 开始根据价格进行相应的支付逻辑处理
            log.info("购买的课程金额为：{}, 学习币为：{}", money, corn);

            return "success";
        }
        return "fail";
    }

    /**
     * description: 测试
     *
     * @author: zhang hang
     **/
    @Test
    public void testBuyCourse() {
        String s = buyCourse(1L, 1L);
    }

}
